home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 July: Mac OS SDK / Dev.CD Jul 97 SDK2.toast / Development Kits (Disc 2) / ScriptX / Documentation / Code Examples from Docs / compguid / evntsdev / selectbl.sx < prev   
Encoding:
Text File  |  1996-05-21  |  2.8 KB  |  98 lines  |  [TEXT/ttxt]

  1. -- <<<-
  2.  
  3. -- selectbl.sx
  4. -- Event Receiver example
  5. -- by Doug Kramer
  6. -- demonstrates receiving mouse events on presenters
  7. -- from "Events and Input Devices" chapter of
  8. -- ScriptX Components Guide
  9.  
  10. module EventTest1 uses ScriptX end
  11. in module EventTest1
  12.  
  13. -- A mixin class
  14. class Selectable (RootObject)
  15.     instance variables
  16.         mouseDown -- For setting up an interest in mouse down events
  17.         mouseUp    -- For setting up an interest in mouse up events
  18.         selectionBox -- Appears around the presenter to highlight it
  19. end
  20.  
  21. -- Define the method that is called when mouse-up occurs
  22. method mouseDownSelect self {class Selectable} theInterest theEvent ->
  23. (
  24.     -- Move the 2D presenter to the front of its container
  25.     moveToFront self.presentedBy self
  26.  
  27.     -- Create a selection rectangle that just fits the 2D presenter
  28.     self.selectionBox := new TwoDShape
  29.         self.selectionBox.target :=(new Rect x2:self.bBox.x2 y2:self.bBox.y2)
  30.         self.selectionBox.stroke := new Brush color:yellowColor
  31.         self.selectionBox.x := self.x
  32.         self.selectionBox.y := self.y
  33.         self.selectionBox.stroke.linewidth := 2
  34.     prepend self.presentedBy self.selectionBox
  35.  
  36.     -- Now we're interested only in mouse up events
  37.     removeEventInterest self.mouseDown
  38.     addEventInterest self.mouseUp
  39.     @accept  -- must accept for event to be swallowed
  40. )
  41.  
  42. -- Define the method that is called when mouse-up occurs
  43. method mouseUpSelect self {class Selectable} theInterest theEvent ->
  44. (
  45.     deleteOne self.presentedBy self.selectionBox
  46.     
  47.     -- now we're interested only in mouse down events
  48.     removeEventInterest self.mouseUp
  49.     addEventInterest self.mouseDown
  50.     @accept -- must accept for event to be swallowed
  51. )
  52.  
  53. method init self {class Selectable} #rest args ->
  54. (
  55.     apply nextMethod self args        -- call init on superclass
  56.  
  57.     -- set up the mouse event interests
  58.     self.mouseDown := new MouseDownEvent
  59.         self.mouseDown.eventReceiver := mouseDownSelect
  60.         self.mouseDown.authorData := self
  61.         self.mouseDown.presenter := self
  62.         
  63.     self.mouseUp := new MouseUpEvent
  64.         self.mouseUp.eventReceiver := mouseUpSelect
  65.         self.mouseUp.authorData := self
  66.         self.mouseUp.presenter := self
  67.         self.mouseUp.matchedInterest := self.mouseDown
  68.         
  69.     -- Initially, we're interested in mouse down events
  70.     addEventInterest self.mouseDown
  71. )
  72.  
  73. -- Set up a simple example of black and red rectangles in a window
  74. global myWindow := new Window boundary:(new Rect x2:300 y2:300)
  75.     myWindow.x := 40
  76.     myWindow.y := 40
  77.     show myWindow
  78.  
  79. -- Create the black rectangle
  80. object myBlackRect (Selectable, TwoDShape)
  81. end
  82.     myBlackRect.target := new Oval x2:100 y2:100 
  83.     myBlackRect.fill := blackBrush
  84.     myBlackRect.x := 75
  85.     myBlackRect.y := 75
  86.     prepend myWindow myBlackRect
  87.  
  88. -- Create the red rectangle
  89. object myRedRect (Selectable, TwoDShape)
  90. end
  91.     myRedRect.target := new Rect x2:100 y2:100
  92.     myRedRect.fill := new Brush color:redColor
  93.     myRedRect.x := 125
  94.     myRedRect.y := 125
  95.     prepend myWindow myRedRect
  96.  
  97. -- >>>
  98.